package com.hk.controller;

import com.github.pagehelper.PageInfo;
import com.hk.commons.CommonResult;
import com.hk.model.ContactUs;
import com.hk.model.vo.Result;
import com.hk.model.vo.ResultVO;
import com.hk.service.IContactUsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: aerfazhe
 * @Date: 2021/7/5 14:45
 * @Statement: 联系我们
 */
@RestController
@RequestMapping("/contactUs")
public class ContactUsController {

    @Autowired
    private IContactUsService iContactUsService;

    @GetMapping("/findAll")
    public ResultVO<ContactUs> findAll(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10")Integer limit,
            ContactUs contactUs
    ) {
        List<ContactUs> contactUsList = iContactUsService.findAll(page, limit, contactUs);
        PageInfo<ContactUs> contactUsPageInfo = new PageInfo<>(contactUsList);
        int count = iContactUsService.count(-1,-1, contactUs);
        if (contactUsList.isEmpty()) {
            ResultVO resultVO = CommonResult.nullResultVO(count,contactUsPageInfo.getList());
            return resultVO;
        }
        ResultVO resultVO = CommonResult.successResultVO(count,contactUsPageInfo.getList());
        return resultVO;
//        return null;
    }

    @PostMapping("/save")
    public Result save(@RequestBody(required = false) ContactUs contactUs) {
        if (contactUs == null) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        int save = iContactUsService.save(contactUs);
        Result result = CommonResult.successResult(null);
        return result;
//        return null;
    }

    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable int id) {
        ContactUs contactUs = iContactUsService.findById(id);
        if (contactUs.getId() == 0) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        Result result = CommonResult.successResult(contactUs);
        return result;
//        return null;
    }

    @PostMapping("/updateMessage")
    public Result updateMessage(@RequestBody(required = false) ContactUs contactUs) {
        if (contactUs == null) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        int update = iContactUsService.update(contactUs);
        Result result = CommonResult.successResult(null);
        return result;
    }

    @PostMapping("/updateState")
    public Result updateState(@RequestBody(required = false) ContactUs contactUs) {
        if (contactUs == null) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        int updateState = iContactUsService.updateState(contactUs);
        Result result = CommonResult.successResult(null);
        return result;
    }

    @PostMapping("/deletetContactUs/{id}")
    public Result deletetContactUs(@PathVariable int id) {
        int delete = iContactUsService.delete(id);
        Result result = CommonResult.successResult(null);
        return result;
    }


}
